Statements Assessment Test

Lets test your knowledge!


Use for, split(), and if to create a Statement that will print out words that start with 's':


In [1]:
st = 'Print only the words that start with s in this sentence'

In [7]:
#Code here 
print st.split()
for word in st.split():
    if word[0] == 's':
        print word


['Print', 'only', 'the', 'words', 'that', 'start', 'with', 's', 'in', 'this', 'sentence']
start
s
sentence

In [11]:
[word for word in st.split() if word[0] == 's']


Out[11]:
['start', 's', 'sentence']

Use range() to print all the even numbers from 0 to 10.


In [13]:
#Code Here
[number for number in xrange(0,11) if number %2 ==0 ]


[0, 2, 4, 6, 8, 10]

Use List comprehension to create a list of all numbers between 1 and 50 that are divisible by 3.


In [15]:
#Code in this cell
[num for num in xrange(1,51) if num % 3 ==0]


Out[15]:
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48]

Go through the string below and if the length of a word is even print "even!"


In [6]:
st = 'Print every word in this sentence that has an even number of letters'

In [17]:
#Code in this cell
for word in st.split():
    if len(word) % 2 == 0:
        print word


only
that
with
in
this
sentence

Write a program that prints the integers from 1 to 100. But for multiples of three print "Fizz" instead of the number, and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".


In [20]:
#Code in this cell
for num in xrange(1,101):
    if num % 3 == 0 and num % 5 == 0:
        print num, 'FizzBuzz'
    elif num % 5 == 0: 
        print num, 'Buzz'
    elif num % 3 ==0:
        print num, 'Fizz'


3 Fizz
5 Buzz
6 Fizz
9 Fizz
10 Buzz
12 Fizz
15 FizzBuzz
18 Fizz
20 Buzz
21 Fizz
24 Fizz
25 Buzz
27 Fizz
30 FizzBuzz
33 Fizz
35 Buzz
36 Fizz
39 Fizz
40 Buzz
42 Fizz
45 FizzBuzz
48 Fizz
50 Buzz
51 Fizz
54 Fizz
55 Buzz
57 Fizz
60 FizzBuzz
63 Fizz
65 Buzz
66 Fizz
69 Fizz
70 Buzz
72 Fizz
75 FizzBuzz
78 Fizz
80 Buzz
81 Fizz
84 Fizz
85 Buzz
87 Fizz
90 FizzBuzz
93 Fizz
95 Buzz
96 Fizz
99 Fizz
100 Buzz

Use List Comprehension to create a list of the first letters of every word in the string below:


In [21]:
st = 'Create a list of the first letters of every word in this string'

In [24]:
#Code in this cell
[word[0] for word in st.split()]


Out[24]:
['C', 'a', 'l', 'o', 't', 'f', 'l', 'o', 'e', 'w', 'i', 't', 's']

In [1]:
l = [2,3,4,74,4,4]

In [ ]:
l.ap

Great Job!